package br.com.main.client.system.botoes;

import java.util.ArrayList;

import br.com.main.client.system.botoes.ReservasPresenter.Display;

import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.HasKeyboardSelectionPolicy.KeyboardSelectionPolicy;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.view.client.SingleSelectionModel;

public class ReservasView extends Composite implements Display{

	private HorizontalPanel panel;
	private CellTable<Reserva> tabela;
	SingleSelectionModel<Reserva> selectionModel;
	ArrayList<Reserva> reservas;
	ListBox lista;
	String[] areas;
	DateBox dateBox = new DateBox();
	Button btRegistrar = new Button("Registrar");

	public ReservasView() {
		init();
		initWidget(panel);
	}
	
	private void init() {
		panel = new HorizontalPanel();
		panel.addStyleName("bordaDados");
		panel.setSpacing(20);
		
		VerticalPanel panelReservas = new VerticalPanel();
		panelReservas.setBorderWidth(1);
		
		panelReservas.add(new Label("Reservas"));
		tabela = new CellTable<Reserva>();
		tabela.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
		selectionModel = new SingleSelectionModel<Reserva>();
		tabela.setSelectionModel(selectionModel);
		
		criarTabela();
		panelReservas.add(tabela);
		
		VerticalPanel panelCadastraReserva = new VerticalPanel();
		panelCadastraReserva.setBorderWidth(1);
		
		panelCadastraReserva.add(new Label("Cadastro de Reserva"));
		lista = new ListBox();
		lista.addStyleName("listBox");
		dateBox.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat("d/MM/y k")));
		panelCadastraReserva.setSpacing(5);
		
		panelCadastraReserva.add(new Label("Selecione uma área"));
		panelCadastraReserva.add(lista);
		panelCadastraReserva.add(new Label("Data (Dia/Mês/Ano Hora)"));
		panelCadastraReserva.add(dateBox);
		panelCadastraReserva.add(btRegistrar);
		
		panel.add(panelReservas);
		panel.add(panelCadastraReserva);
	}
	
	@Override
	public ListBox getLista() {
		return lista;
	}
	@Override
	public DateBox getDateBox() {
		return dateBox;
	}

	@Override
	public Button getBtRegistrar() {
		return btRegistrar;
	}

	public void criarTabela() {
		TextColumn<Reserva> colCpfCond = new TextColumn<Reserva>() {
			
			@Override
			public String getValue(Reserva object) {
				return object.getCpfCondomino();
			}
		};
		
		tabela.addColumn(colCpfCond,"Cpf do condômino");
		
		TextColumn<Reserva> colNomeCond = new TextColumn<Reserva>() {

			@Override
			public String getValue(Reserva object) {
				return object.getNomeCondomino();
			}
		};
		
		tabela.addColumn(colNomeCond,"Nome");
		
		TextColumn<Reserva>colNomeArea = new TextColumn<Reserva>() {
			
			@Override
			public String getValue(Reserva object) {
				return object.getNomeArea();
			}
		};
		
		tabela.addColumn(colNomeArea,"Nome Local");
		
		TextColumn<Reserva>colDataRes = new TextColumn<Reserva>() {

			@Override
			public String getValue(Reserva object) {
				return object.getDataReserva();
			}
		};
		
		tabela.addColumn(colDataRes,"Data Reservada");
	}
	
	@Override
	public void atualizaReservas(ArrayList<Reserva> res) {
		this.reservas = res;
	}
	
	@Override
	public void atualizaTabela() {
		tabela.setRowCount(reservas.size());
		tabela.setRowData(0, reservas);
	}
	
	@Override
	public void preencheLista(String[] a) {
		for (int i = 0; i < a.length; i++) {
			lista.addItem(a[i]);
		}
	}
}
